package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaColumn;
import com.wudl.govern.service.MetaColumnService;
import com.wudl.govern.vo.MetaColumnVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * 字段 Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaColumn")
public class MetaColumnController {

    @Autowired
    private MetaColumnService metaColumnService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaColumnVO>> page(@RequestBody MetaColumnVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaColumnVO> MetaColumnVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaColumn> MetaColumnList = metaColumnService.page(page, size);

        for (MetaColumn metaColumnTmp:MetaColumnList )
        {

            MetaColumnVoList.add(BeanUtil.copyProperties(metaColumnTmp, MetaColumnVO.class));
        }
        return CommonResult.success(MetaColumnVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaColumnVO> get(@PathVariable Object id) {
        MetaColumn entity = metaColumnService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaColumnVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaColumnVO addVO) {
        metaColumnService.save(BeanUtil.copyProperties(addVO, MetaColumn.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaColumnVO editVO) {
        metaColumnService.updateById(BeanUtil.copyProperties(editVO, MetaColumn.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Object id) {
        metaColumnService.removeById(id);
        return CommonResult.success();
    }

}
